package com.ldg.seckill.controller;

import com.ld.utils.MyDate;
import com.ldg.seckill.entity.TbSeckillGoods;
import com.ldg.seckill.service.TbSeckillGoodsService;
import com.ldg.seckill.service.TbSeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-10-21-17:09
 * @Description:
 */
@Controller
public class SecKillGoodsController {
    @Autowired
    private TbSeckillGoodsService goodsService;

    @RequestMapping({"/","/index"})
    public String index(Model model){
        //得到当前时间开始的五个时间段
        List<Date> dateMenus = MyDate.getDateMenus();
        //放到model中
        model.addAttribute("dateMenus",dateMenus);
        //根据时间段查询符合的商品
        List<TbSeckillGoods> goodslist = goodsService.findSeckillGoodsByDate(MyDate.getDateStr(MyDate.getDateMenus().get(0)));
        //放到model中
        model.addAttribute("goodslist",goodslist);
        return "seckill-index";
    }

    @GetMapping("findSeckillGoods/{secKillId}")
    public String findSeckillGoods(@PathVariable String secKillId, Model model){
        TbSeckillGoods seckillGoods = goodsService.findById(secKillId);
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";
    }
}
